<template>
	<view class="fui-wrap">
		<fui-list>
			<fui-list-cell v-for="(item,index) in list" :key="index">
				<view class="item">
					<view class="line">
						<fui-text text="车牌号：" color="#afafaf" :size="28" align="right" block style="width: 140rpx;"></fui-text>
						<fui-text :text="item.plate_number" color="black" :size="28"></fui-text>
					</view>
					<view class="line">
						<fui-text text="车牌颜色：" color="#afafaf" :size="28" align="right" block style="width: 140rpx;"></fui-text>
						<fui-text :text="item.plate_type" color="black" :size="28"></fui-text>
					</view>
					<view class="line">
						<fui-text text="车辆类型：" color="#afafaf" :size="28" align="right" block style="width: 140rpx;"></fui-text>
						<fui-text :text="item.car_models" color="black" :size="28"></fui-text>
					</view>
				</view>
				<view class="del">
					<fui-button height="50rpx" text="删除" type="danger" @click="del(item,index)"></fui-button>
				</view>
			</fui-list-cell>
		</fui-list>
		<fui-empty v-if="list.length===0" :src="`${baseUrl}assets/img/miniapp/empty.png`" title="暂无数据!" :marginTop="200"></fui-empty>
		<fui-fab @click="add" position="right" :bottom="120" width="70" distance="30">
			<fui-icon name="plus" color="#fff" size="60"></fui-icon>
		</fui-fab>
		<filter :param="param" @confirm="doSubmit"></filter>
		<fui-modal width="600" v-if="showAddPlates" :show="showAddPlates" @click="clickButton">
			<fui-form error-position="1" labelColor="#7a7a7a" top="0" :model="plates">
				<fui-form-item label="车牌号" arrow prop="plate_number">
					<fui-input readonly @click="$navigateTo('parking/plate?type=input&plate_number='+plates.plate_number)" :borderBottom="false" :padding="[0]" placeholder="请输入车牌号" v-model="plates.plate_number"></fui-input>
				</fui-form-item>
				<fui-form-item label="车牌颜色" arrow prop="plate_type">
					<fui-input readonly @click="showPlateType=true" :borderBottom="false" :padding="[0]" :value="plate_type[plates.plate_type]"></fui-input>
				</fui-form-item>
				<fui-form-item label="车辆类型" arrow prop="car_models">
					<fui-input readonly @click="showCarModels=true" :borderBottom="false" :padding="[0]" :value="car_models[plates.car_models]"></fui-input>
				</fui-form-item>
			</fui-form>
		</fui-modal>
		<fui-picker :fields ="['title','id']" :layer="1" :linkage="true" :options="parseColumns(car_models)" v-if="showCarModels" :show="showCarModels" @change="changePicker" @cancel="cancelPicker"></fui-picker>
		<fui-picker :fields ="['title','id']" :layer="1" :linkage="true" :options="parseColumns(plate_type)" v-if="showPlateType" :show="showPlateType" @change="changePicker" @cancel="cancelPicker"></fui-picker>
	</view>
</template>

<script>
	import {methods,baseUrl} from '@/utils/core.js';
	import filter from '@/components/common/filter.vue';
	export default {
		components:{
			filter:filter
		},
		data() {
			return {
				baseUrl:baseUrl,
				cars_id:'',
				list:[],
				page:1,
				endData:false,
				param:{
					plate_number:''
				},
				showAddPlates:false,
				plates:{
					plate_number:'',
					plate_type:'blue',
					car_models:'car'
				},
				car_models:[],
				plate_type:[],
				showPlateType:false,
				showCarModels:false
			}
		},
		onLoad(e) {
			this.cars_id=e.cars_id;
			this.getInfo().then(res=>{
				this.car_models=res.car_models;
				this.plate_type=res.plate_type;
				this.getList();
			});
		},
		onShow() {
			let plate_number=uni.getStorageSync('plate_number') || '';
			if(plate_number){
				this.plates.plate_number=plate_number;
				uni.setStorageSync('plate_number','');
			}
		},
		onReachBottom() {
			this.getList();
		},
		onPullDownRefresh() {
			this.list=[];
			this.page=1;
			this.endData=false;
			this.getList(function(){
				uni.stopPullDownRefresh();
			});
		},
		methods: {
			...methods,
			doSubmit:function(param){
				this.param=param;
				this.list=[];
				this.page=1;
				this.endData=false;
				this.getList();
			},
			parseColumns:function(data){
				let r=[];
				for(let key in data){
					r.push({
						id:key,
						title:data[key]
					});
				}
				return r;
			},
			getInfo:function(){
				return this.$get('parking/cars/info','',true);
			},
			del:function(e,index){
				uni.showModal({
					content:'你确定要删除【'+e.plate_number+'】吗？',
					success: (r) => {
						if(r.confirm){
							this.$post('parking/cars/delplate',{cars_id:this.cars_id,id:e.id},true,true).then(res=>{
								this.list.splice(index,1);
							});
						}
					}
				})
			},
			add:function(){
				this.showAddPlates=true;
			},
			changePicker:function(e){
				if(this.showPlateType){
					this.plates.plate_type=e.value;
					this.showPlateType=false;
				}
				if(this.showCarModels){
					this.plates.car_models=e.value;
					this.showCarModels=false;
				}
			},
			cancelPicker:function(){
				this.showPlateType=false;
				this.showCarModels=false;
			},
			clickButton:function(e){
				if(e.index){
					if(!this.plates.plate_number){
						this.$msg('车牌号不能为空');
						return;
					}
					this.$post('parking/cars/addplate',{cars_id:this.cars_id,...this.plates},true,true).then(res=>{
						this.list.unshift(res);
						this.showAddPlates=false;
					});
				}else{
					this.showAddPlates=false;
				}
			},
			getList:function(callback=false){
				if(this.endData){
					this.$msg('已经到底了');
					return;
				}
				this.$get('parking/cars/plates',{
					...this.param,
					cars_id:this.cars_id,
					page:this.page
				},true).then(res=>{
					this.list=this.list.concat(res);
					this.page++;
					if(res.length<10){
						this.endData=true;
					}	
					if(callback){
						callback();
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
.item{
	display: flex;
	flex-wrap: wrap;
	width: 70%;
	.line{
		display: flex;
		align-items: center;
		width:100%;
		height: 70rpx;		
	}
}
.del{
	width: 110rpx;
}
</style>